package com.springdoor.goods.service.impl;

import java.util.Iterator;
import java.util.List;

import com.springdoor.common.constant.UserConstants;
import com.springdoor.common.core.domain.TreeSelect;
import com.springdoor.goods.mapper.SprdGoodsAttrValMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

import com.springdoor.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.springdoor.common.core.domain.entity.SprdGoodsAttrVal;
import com.springdoor.goods.mapper.SprdGoodsAttrMapper;
import com.springdoor.common.core.domain.entity.SprdGoodsAttr;
import com.springdoor.goods.service.ISprdGoodsAttrService;

/**
 * 商品属性Service业务层处理
 *
 * @author llshs
 * @date 2024-05-08
 */
@Service
public class SprdGoodsAttrServiceImpl implements ISprdGoodsAttrService
{
    @Autowired
    private SprdGoodsAttrMapper sprdGoodsAttrMapper;

    @Autowired
    private SprdGoodsAttrValMapper sprdGoodsAttrValMapper;

    /**
     * 查询商品属性
     *
     * @param attrId 商品属性主键
     * @return 商品属性
     */
    @Override
    public SprdGoodsAttr selectSprdGoodsAttrByAttrId(Long attrId)
    {
        return sprdGoodsAttrMapper.selectSprdGoodsAttrByAttrId(attrId);
    }

    /**
     * 查询商品属性列表
     *
     * @param sprdGoodsAttr 商品属性
     * @return 商品属性
     */
    @Override
    public List<SprdGoodsAttr> selectSprdGoodsAttrList(SprdGoodsAttr sprdGoodsAttr)
    {
        return sprdGoodsAttrMapper.selectSprdGoodsAttrList(sprdGoodsAttr);
    }

    /**
     * 新增商品属性
     *
     * @param sprdGoodsAttr 商品属性
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSprdGoodsAttr(SprdGoodsAttr sprdGoodsAttr)
    {
        int rows = sprdGoodsAttrMapper.insertSprdGoodsAttr(sprdGoodsAttr);
        insertSprdGoodsAttrVal(sprdGoodsAttr);
        return rows;
    }

    /**
     * 修改商品属性
     *
     * @param sprdGoodsAttr 商品属性
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSprdGoodsAttr(SprdGoodsAttr sprdGoodsAttr)
    {
        sprdGoodsAttrValMapper.deleteSprdGoodsAttrValByAttrId(sprdGoodsAttr.getAttrId());
        insertSprdGoodsAttrVal(sprdGoodsAttr);
        return sprdGoodsAttrMapper.updateSprdGoodsAttr(sprdGoodsAttr);
    }

    /**
     * 批量删除商品属性
     *
     * @param attrIds 需要删除的商品属性主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSprdGoodsAttrByAttrIds(Long[] attrIds)
    {
        sprdGoodsAttrValMapper.deleteSprdGoodsAttrValByAttrIds(attrIds);
        return sprdGoodsAttrMapper.deleteSprdGoodsAttrByAttrIds(attrIds);
    }

    /**
     * 删除商品属性信息
     *
     * @param attrId 商品属性主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSprdGoodsAttrByAttrId(Long attrId)
    {
        sprdGoodsAttrValMapper.deleteSprdGoodsAttrValByAttrId(attrId);
        return sprdGoodsAttrMapper.deleteSprdGoodsAttrByAttrId(attrId);
    }

    /**
     * 新增商品属性值信息
     *
     * @param sprdGoodsAttr 商品属性对象
     */
    public void insertSprdGoodsAttrVal(SprdGoodsAttr sprdGoodsAttr)
    {
        List<SprdGoodsAttrVal> sprdGoodsAttrValList = sprdGoodsAttr.getSprdGoodsAttrValList();
        Long attrId = sprdGoodsAttr.getAttrId();
        if (StringUtils.isNotNull(sprdGoodsAttrValList))
        {
            List<SprdGoodsAttrVal> list = new ArrayList<SprdGoodsAttrVal>();
            for (SprdGoodsAttrVal sprdGoodsAttrVal : sprdGoodsAttrValList)
            {
                sprdGoodsAttrVal.setAttrId(attrId);
                sprdGoodsAttrVal.setUpdateBy(sprdGoodsAttr.getUpdateBy());
                sprdGoodsAttrVal.setCreateBy(sprdGoodsAttr.getUpdateBy());
                list.add(sprdGoodsAttrVal);
            }
            if (list.size() > 0)
            {
                sprdGoodsAttrValMapper.batchSprdGoodsAttrVal(list);
            }
        }
    }

    @Override
    public boolean checkGoodsAttrCodeUnique(SprdGoodsAttr goodsAttr) {
        Long attrId = StringUtils.isNull(goodsAttr.getAttrId()) ? -1L : goodsAttr.getAttrId();
        SprdGoodsAttr info = sprdGoodsAttrMapper.checkGoodsAttrCodeUnique(goodsAttr);
        if (StringUtils.isNotNull(info) && info.getAttrId().longValue() != attrId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean checkGoodsAttrNameUnique(SprdGoodsAttr goodsAttr) {
        Long attrId = StringUtils.isNull(goodsAttr.getAttrId()) ? -1L : goodsAttr.getAttrId();
        SprdGoodsAttr info = sprdGoodsAttrMapper.checkGoodsAttrNameUnique(goodsAttr);
        if (StringUtils.isNotNull(info) && info.getAttrId().longValue() != attrId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public List<SprdGoodsAttr> selectGoodsAttrAll() {
        return sprdGoodsAttrMapper.selectGoodsAttrAll();
    }

    @Override
    public List<SprdGoodsAttr> selectGoodsAttrMenu() {
        return sprdGoodsAttrMapper.selectGoodsAttrMenu();
    }

    @Override
    public List<SprdGoodsAttr> selectValidGoodsAttrAll() {
        return sprdGoodsAttrMapper.selectValidGoodsAttrAll();
    }



    /**
     * 构建前端所需要下拉树结构
     *
     * @param list
     * @return
     */
    @Override
    public List<TreeSelect> buildAttrTreeSelect(List<SprdGoodsAttr> list) {
        List<SprdGoodsAttr> trees = buildAttrTree(list);
        return trees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Long> selectAttrListByTypeId(Long typeId) {
        return sprdGoodsAttrMapper.selectAttrListByTypeId(typeId);
    }

    @Override
    public List<SprdGoodsAttr> findAttrsWithoutConfig(Map<String, Object> param) {
        return sprdGoodsAttrMapper.findAttrsWithoutConfig(param);
    }

    @Override
    public List<SprdGoodsAttr> selectTypeAttrListByTypeId(Long typeId) {
        return sprdGoodsAttrMapper.selectTypeAttrListByTypeId(typeId);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param attrs 属性列表
     * @return 树结构列表
     */
    private List<SprdGoodsAttr> buildAttrTree(List<SprdGoodsAttr> attrs) {
        List<SprdGoodsAttr> returnList = new ArrayList<SprdGoodsAttr>();
        List<Long> tempList = attrs.stream().map(SprdGoodsAttr::getAttrId).collect(Collectors.toList());
        for (Iterator<SprdGoodsAttr> iterator = attrs.iterator(); iterator.hasNext();)
        {
            SprdGoodsAttr attr = (SprdGoodsAttr) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(attr.getParentId()))
            {
                recursionFn(attrs, attr);
                returnList.add(attr);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = attrs;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list 分类表
     * @param a 子节点
     */
    private void recursionFn(List<SprdGoodsAttr> list, SprdGoodsAttr a)
    {
        // 得到子节点列表
        List<SprdGoodsAttr> childList = getChildList(list, a);
        a.setChildren(childList);
        for (SprdGoodsAttr tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SprdGoodsAttr> getChildList(List<SprdGoodsAttr> list, SprdGoodsAttr a)
    {
        List<SprdGoodsAttr> tlist = new ArrayList<SprdGoodsAttr>();
        Iterator<SprdGoodsAttr> it = list.iterator();
        while (it.hasNext())
        {
            SprdGoodsAttr n = (SprdGoodsAttr) it.next();
            if (n.getParentId().longValue() == a.getAttrId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SprdGoodsAttr> list, SprdGoodsAttr t)
    {
        return getChildList(list, t).size() > 0;
    }
}
